package com.imachen.ymcc.schedule;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ctc.wstx.util.DataUtil;
import com.imachen.ymcc.domain.KillActivity;
import com.imachen.ymcc.enums.GlobalExceptionCode;
import com.imachen.ymcc.mapper.KillActivityMapper;
import com.imachen.ymcc.service.IKillActivityService;
import com.imachen.ymcc.util.AssertUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
@Slf4j
@Component
public class killCoursePublishSchedule {


    @Autowired
    private IKillActivityService killActivityService;

    // 0 0 2 * * ? 每天凌晨两点  0 0/2 * * * ?每隔两分钟
    @Scheduled(cron = "0 0/2 * * * ?")
    public void configureTasks(){
        Date now = new Date();
        log.info("定时器任务执行时间:" + now);

        Wrapper<KillActivity> wrapper = new EntityWrapper<>();
        wrapper.eq("publish_status",0);
        wrapper.gt("start_time",now).and()
                .le("start_time",DateUtils.addHours(now,2));
        List<KillActivity> killActivities = killActivityService.selectList(wrapper);
        AssertUtil.isNotNull(killActivities, GlobalExceptionCode.ERROR);
        killActivities.forEach(killActivity -> {
            killActivityService.publish(killActivity.getId());
            log.info("秒杀活动发布成功,活动名：" + killActivity.getName());
        });

    }

}
